package table.col;

import java.util.Iterator;
import java.util.List;

import table.Col;
import table.Row;
import table.node.BaseNode;

public class FormulaDiscountCol extends Col {
	private String QuotaColName;
	private String QuantityColName;
	private String DiscountColName;

	
	public void setQuotaColName(String quotaColName) {
		QuotaColName = quotaColName;
	}
	public String getQuotaColName() {
		return QuotaColName;
	}
	public void setQuantityColName(String quantityColName) {
		QuantityColName = quantityColName;
	}
	public String getQuantityColName() {
		return QuantityColName;
	}


	public void setDiscountColName(String discountColName) {
		DiscountColName = discountColName;
	}
	public String getDiscountColName() {
		return DiscountColName;
	}

	
	
	public void setDataByCols(List<Col> cols){
		
		if( 0 < cols.size() && cols.get(0) != null && cols.get(0).getDataSize() > 0){
			if( 0 < cols.size() && cols.get(0) != null && cols.get(0).getDataSize() > 0){
				int dataSize = cols.get(0).getDataSize();
				for(int i=0; i < dataSize; i++){
					BaseNode dataNode = new BaseNode();
					try{
						dataNode.setNodeValue(this.calc(this.getRowByColsIndex(cols, i)) );
					}catch(Exception e){
						dataNode.setNodeValue("Error!");
					}
					this.addData(dataNode);
				}
			}
		}
	}
	
	protected Double calc(Row dataRow){

		Double result = 0.0;
		Double Quota = Double.parseDouble(dataRow.getValueByName(this.getQuotaColName()).toString());
		Double Quantity = Double.parseDouble(dataRow.getValueByName(this.getQuantityColName()).toString());
		Double Discount = Double.parseDouble(dataRow.getValueByName(this.getDiscountColName()).toString());
		Quota = 5.3/28.4*Quota;
		if(Quantity > Quota){
			result = ((Quantity - Quota)*Discount + Quota)/Quantity;
		} else {
			result = 1.0;
		}
		return result;
	}
	
	protected Row getRowByColsIndex(List<Col> cols, int rowIndex){
		try{
			Row dataRow = new Row();
			for(Iterator<Col> it = cols.iterator();it.hasNext();){
				Col tCol = it.next(); 
				dataRow.setValueByName(tCol.getName(), tCol.getValue(rowIndex));
			}
			return dataRow;
		}catch(Exception e){
		}
		return null;
	}
	
}
